package com.esunny.resourcevisualization.service.impl;

import com.esunny.resourcevisualization.domain.entity.ResourceTypeField;
import com.esunny.resourcevisualization.domain.response.ReturnStructure;
import com.esunny.resourcevisualization.manage.ResourceTypeFieldEntityDataManage;
import com.esunny.resourcevisualization.manage.ResourceTypeFieldManage;
import com.esunny.resourcevisualization.service.ResourceTypeFieldService;
import com.esunny.resourcevisualization.util.ConstantUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0.0.0
 * @ClassName ResourceTypeFieldEntityDataManageImpl
 * @Description 资源类别服务
 * @Author baochencong
 * @Date 2020/9/28 13:11
 */
@Service
public class ResourceTypeFieldServiceImpl implements ResourceTypeFieldService {

    @Resource
    private ResourceTypeFieldEntityDataManage resourceTypeFieldEntityDataManage;

    @Resource
    private ResourceTypeFieldManage resourceTypeFieldManage;

    @Override
    public ReturnStructure addResourceTypeField(@NotNull ResourceTypeField resourceTypeField) {
        String name = resourceTypeField.getResourceTypeName();
        String fieldName = resourceTypeField.getResourceTypeFieldName();
        if (resourceTypeFieldExistedByNameAndFieldName(name, fieldName)) {
            return ReturnStructure.ERROR("字段英文名(" + resourceTypeField.getResourceTypeFieldName() + ")已存在", null);
        }
        if (resourceTypeFieldExistedByNameAndFieldTitle(name, resourceTypeField.getResourceTypeFieldTitle())) {
            return ReturnStructure.ERROR("字段中文名(" + resourceTypeField.getResourceTypeFieldTitle() + ")已存在", null);
        }
        return resourceTypeFieldManage.addResourceTypeField(resourceTypeField);
    }

    @Override
    public ReturnStructure addResourceTypeFieldList(@NotNull List<ResourceTypeField> resourceTypeFieldList) {
        // 要么全部字段都添加，要么都不添加
        List<String> errorInfo = new ArrayList<>();
        // 判断是否都可以添加
        for (ResourceTypeField field : resourceTypeFieldList) {
            ReturnStructure returnStructure = addResourceTypeFieldValid(field);
            if (returnStructure.getCode() == ConstantUtil.HTTP_FAILED_CODE) {
                errorInfo.add(returnStructure.getMessage());
            }
        }
        if (errorInfo.size() > 0) {
            return ReturnStructure.ERROR("添加资源类别字段失败", errorInfo);
        } else {
            for (ResourceTypeField field : resourceTypeFieldList) {
                addResourceTypeField(field);
            }
            return ReturnStructure.SUCCESS("添加资源类别字段成功", null);
        }
    }

    private ReturnStructure addResourceTypeFieldValid(ResourceTypeField resourceTypeField) {
        if (resourceTypeFieldExistedByNameAndFieldName(resourceTypeField.getResourceTypeName(), resourceTypeField.getResourceTypeFieldName())) {
            return ReturnStructure.ERROR("字段英文名(" + resourceTypeField.getResourceTypeFieldName() + ")已存在", null);
        }
        if (resourceTypeFieldExistedByNameAndFieldTitle(resourceTypeField.getResourceTypeName(), resourceTypeField.getResourceTypeFieldTitle())) {
            return ReturnStructure.ERROR("字段中文名(" + resourceTypeField.getResourceTypeFieldTitle() + ")已存在", null);
        }
        return ReturnStructure.SUCCESS("", null);
    }

    @Override
    public ReturnStructure deleteResourceTypeFieldByFieldId(int fieldId) {
        if (isExistedResourceTypeFieldByField(fieldId)) {
            return resourceTypeFieldEntityDataManage.deleteResourceTypeFieldInfoByFieldId(fieldId);
        }
        return ReturnStructure.ERROR("该资源类别字段不存在", null);
    }

    @Override
    public ReturnStructure updateResourceTyprFieldByFieldId(ResourceTypeField resourceTypeField) {
        int fieldId = resourceTypeField.getResourceTypeFieldId();
        if (isExistedResourceTypeFieldByField(fieldId)) {
            if (updateResourceTypeFieldNameFailed(resourceTypeField, fieldId)) {
                return ReturnStructure.ERROR("资源类别字段英文名已存在", null);
            }
            if (updateResourceTypeFieldTitleFailed(resourceTypeField, fieldId)) {
                return ReturnStructure.ERROR("资源类别字段中文名已存在", null);
            }
            ResourceTypeField oldResourceTypeFieldId = resourceTypeFieldManage.getResourceTypeFieldId(fieldId);
            String oldFieldName = oldResourceTypeFieldId.getResourceTypeFieldName();
            String newFieldName = resourceTypeField.getResourceTypeFieldName();
            if (oldFieldName.equals(newFieldName)) {
                return resourceTypeFieldManage.updateResourceTyprFieldByFieldId(resourceTypeField);
            }
            return resourceTypeFieldEntityDataManage.updateResourceTypeFieldInfo(oldFieldName, resourceTypeField);
        }
        return ReturnStructure.ERROR("该资源类别字段不存在", null);
    }

    @Override
    public ResourceTypeField getResourceTypeFieldByField(int fieldId) {
        return resourceTypeFieldManage.getResourceTypeFieldId(fieldId);
    }

    @Override
    public List<ResourceTypeField> getResourceTypeFieldByName(String name) {
        return resourceTypeFieldManage.getResourceTypeFieldByName(name);
    }

    @Override
    public ResourceTypeField getResourceTypeFieldByNameAndFieldName(String name, String fieldName) {
        return resourceTypeFieldManage.getResourceTypeFieldByNameAndFieldName(name, fieldName);
    }

    /**
     * 判断是否可以更新字段英文名
     *
     * @param resourceTypeField 待更新数据
     * @param field             字段id
     * @return 是否不可以更新
     */
    private boolean updateResourceTypeFieldNameFailed(@NotNull ResourceTypeField resourceTypeField, int field) {
        if (resourceTypeFieldManage.getResourceTypeFieldId(field).getResourceTypeFieldName().equals(resourceTypeField.getResourceTypeFieldName())) {
            return false;
        } else {
            return resourceTypeFieldExistedByNameAndFieldName(resourceTypeField.getResourceTypeName(), resourceTypeField.getResourceTypeFieldName());
        }
    }

    /**
     * 判断是否可以更新字段中文名
     *
     * @param resourceTypeField 待更新数据
     * @param field             字段id
     * @return 是否不可以更新
     */
    private boolean updateResourceTypeFieldTitleFailed(ResourceTypeField resourceTypeField, int field) {
        if (resourceTypeFieldManage.getResourceTypeFieldId(field).getResourceTypeFieldTitle().equals(resourceTypeField.getResourceTypeFieldTitle())) {
            return false;
        } else {
            return resourceTypeFieldExistedByNameAndFieldTitle(resourceTypeField.getResourceTypeName(), resourceTypeField.getResourceTypeFieldTitle());
        }
    }

    /**
     * 通过资源类别英文名和资源类别字段英文名判断资源类别字段是否存在
     *
     * @param name      资源类别英文名
     * @param fieldName 资源类别字段英文名
     * @return 是否存在
     */
    private boolean resourceTypeFieldExistedByNameAndFieldName(String name, String fieldName) {
        return resourceTypeFieldManage.getResourceTypeFieldByNameAndFieldName(name, fieldName) != null;
    }

    /**
     * 通过资源类别英文名和资源类别字段中文名判断资源类别字段是否存在
     *
     * @param name       资源类别英文名
     * @param fieldTitle 资源类别字段中文名
     * @return 是否存在
     */
    private boolean resourceTypeFieldExistedByNameAndFieldTitle(String name, String fieldTitle) {
        return resourceTypeFieldManage.getResourceTypeFieldByNameAndFieldTitle(name, fieldTitle) != null;
    }

    /**
     * 通过资源类别字段id判断资源类别字段是否存在
     *
     * @param fieldId 资源类别id
     * @return 是否存在
     */
    private boolean isExistedResourceTypeFieldByField(int fieldId) {
        return resourceTypeFieldManage.getResourceTypeFieldId(fieldId) != null;
    }
}
